package com.young.grpc.server;

import io.grpc.Server;
import io.grpc.netty.NettyServerBuilder;

/**
 * @description：
 * @author: yangyahui01
 * @date: 12/3/24 4:51 PM
 */
public class ServerBootStrap {

    Server server;

    public static void main(String[] args) throws Exception {


        ServerBootStrap serverBootStrap = new ServerBootStrap();

        serverBootStrap.start();

        serverBootStrap.blockUntilShutdown();


    }

    void start() throws Exception{
        server = NettyServerBuilder.forPort(50091).addService(new ProductInfoImpl())
                .build()
                .start();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                ServerBootStrap.this.stop();
            }
        });
    }

    private void stop() {
        if (server != null) {
            server.shutdown();
        }
    }

    private void blockUntilShutdown() throws InterruptedException {
        if (server != null) {
            server.awaitTermination();
        }
    }
}
